package com.ruoyi.web.controller.common;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping(value = "/addTime")
public class addTimeController {


    @Resource
    private SysKeyNzMapper sysKeyNzMapper;
    @Resource
    private SysKeyYkMapper sysKeyYkMapper;

    @Resource
    private SysKeyMapper sysKeyMapper;
    @Resource
    private SysUserAccountMapper sysUserAccountMapper;

    @Resource
    private SysKeyPcMapper sysKeyPcMapper;

    @Resource
    private SysKeyDidaMapper sysKeyDidaMapper;





    @GetMapping(value = "/add")
    private void addTime() {
        List<SysKeyYk> sysKeyNzs = sysKeyYkMapper.selectAllKey();
        for (SysKeyYk sysKeyNz : sysKeyNzs) {
            if (sysKeyNz.getExpirationTime() == null) {
                continue;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String endTime = simpleDateFormat.format(sysKeyNz.getExpirationTime());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(endTime, formatter);
            LocalDateTime newDateTime = dateTime.plusDays(5);
            String newDateTimeStr = newDateTime.format(formatter);
            try {
                sysKeyNz.setExpirationTime(simpleDateFormat.parse(newDateTimeStr));
                System.out.println("加一天后的时间为：" + newDateTimeStr);
                sysKeyYkMapper.updateSysKeyYk(sysKeyNz);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }


        // 百里
//        List<SysUserAccount> sysUserAccounts = sysUserAccountMapper.selectAllAccount();
//        for (SysUserAccount sysUserAccount : sysUserAccounts) {
//            if (sysUserAccount.getEndTime() == null || sysUserAccount.getEndTime().isEmpty()){
//                continue;
//            }
//            // 对当前时间加一天
//            String endTime = sysUserAccount.getEndTime();
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//            LocalDateTime dateTime = LocalDateTime.parse(endTime, formatter);
//            LocalDateTime newDateTime = dateTime.plusDays(2);
//            String newDateTimeStr = newDateTime.format(formatter);
//            System.out.println("加一天后的时间为：" + newDateTimeStr);
//            sysUserAccount.setEndTime(endTime);
//            sysUserAccountMapper.updateSysUserAccount(sysUserAccount);
//        }
        System.out.printf("修改完成");
    }

    @GetMapping(value = "/noBind")
    private String noBind(String type, String keyNumber) {
        if (type.equals("BL")) {
            SysKey sysKey = sysKeyMapper.selectSysKeyByKey(keyNumber);
            if (sysKey == null){
                return "查无此卡~";
            }

            String account = sysKey.getAccount();
            if (account.equals("未使用")){
                return "该卡密未被使用~";
            }
           int i =  sysUserAccountMapper.noBind(account);
            if (i > 0){
                return "百里解绑成功~";
            }else {
                return "百里解绑失败~";
            }
        }
        if (type.equals("JT")) {
            SysKeyNz sysKey = sysKeyNzMapper.selectSysKeyNzByKey(keyNumber);
            if (sysKey == null){
                return "查无此卡~";
            }
            String account = sysKey.getPersonName();
            if (account == null || account.isEmpty()){
                return "该卡密未被使用~";
            }
            int i =  sysKeyNzMapper.noBindTwo(keyNumber);
            if (i > 0){
                return "鸡腿解绑成功~";
            }else {
                return "鸡腿解绑失败~";
            }
        }

        if (type.equals("XDD")) {
            SysKeyYk sysKey = sysKeyYkMapper.selectSysKeyYkByKey(keyNumber);
            if (sysKey == null){
                return "查无此卡~";
            }
            String account = sysKey.getPersonName();
            if (account == null || account.isEmpty()){
                return "该卡密未被使用~";
            }
            int i =  sysKeyYkMapper.noBindTwo(keyNumber);
            if (i > 0){
                return "小叮当解绑成功~";
            }else {
                return "小叮当解绑失败~";
            }
        }
        if (type.equals("WL")) {
            SysKeyPC sysKey = sysKeyPcMapper.selectSysKeyNzByKey(keyNumber);
            if (sysKey == null){
                return "查无此卡~";
            }
            String account = sysKey.getPersonName();
            if (account == null || account.isEmpty()){
                return "该卡密未被使用~";
            }
            int i =  sysKeyPcMapper.noBindTwo(keyNumber);
            if (i > 0){
                return "未来解绑成功~";
            }else {
                return "未来解绑失败~";
            }
        }
        if (type.equals("XMB")) {
            SysKeyDida sysKey = sysKeyDidaMapper.selectSysKeyNzByKey(keyNumber);
            if (sysKey == null){
                return "查无此卡~";
            }
            String account = sysKey.getPersonName();
            if (account == null || account.isEmpty()){
                return "该卡密未被使用~";
            }
            int i =  sysKeyDidaMapper.noBindTwo(keyNumber);
            if (i > 0){
                return "小目标解绑成功~";
            }else {
                return "小目标解绑失败~";
            }
        }
        return "ok";
    }


}
